/**
 * 客户资源录入脚本支持
 */
var key = null;
var clientKey = null;
var modifydata = null;

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 初始化
	button_bind();
	init_editors();
	client_detailed( $( "#tbl_client" ), false );
	client_detailed( $( "#tbl_payment" ), true );

	// 初始化选择
	if( !empty( clientKey ) && !empty( modifydata ) )
	{
		clientSelectedHandler( modifydata );
		setPaymentData( modifydata );
	}
} );

//--------------------------------------------------------------------------
//
//	consultant_client.php
//
//--------------------------------------------------------------------------

var _editor = [];

function button_bind()
{
	$( "#btnSelect" ).bind( "click", function()
	{
		parent.clienter.setCallback( clientSelectedHandler );
		parent.clienter.reset();
		parent.clienter.open();
	} );

	$( "#btnSubmit" ).bind( "click", function()
	{
		submit_form();
	} );

	$( "#btn_detailed" ).click( function( e )
	{
		e.stopPropagation();

		var show = $( "#tbl_client" ).find( "tr[rel='hide']" ).eq( 0 ).visible();
		client_detailed( $( "#tbl_client" ), !show );
	} );

	$( "#cost" ).blur( ipt_blur );
	$( "#cost" ).keyup( ipt_blur );
	$( "#discount" ).blur( ipt_blur );
	$( "#discount" ).keyup( ipt_blur );

	$( "#xltype" ).change( function()
	{
		var val = $( this ).val();
		client_detailed( $( "#tbl_payment" ), val == 1 ? false : true );
	} );
}

/** 当数值输入框失去焦点时，去除非数值 * */
function ipt_blur()
{
	var $ipt = $( this );
	var val = $ipt.val();

	val = val.replace( /^[0]+/ig, "" );
	val = val.replace( /[^\d]+/ig, "" );

	$ipt.val( val );
}

function init_editors()
{
	$( "div[contenteditable='true']" ).each( function()
	{
		if( _editor.indexOf( $( this ).attr( "id" ) ) == -1 )
			_editor.push( $( this ).attr( "id" ) );
	} );
}

function client_detailed( tar, visible )
{
	if( visible )
		tar.find( "tr[rel='hide']" ).show();
	else
		tar.find( "tr[rel='hide']" ).hide();

	setTableRowBackColor( tar );
}

//--------------------------------------------------------------------------
//
//	数据提交
//
//--------------------------------------------------------------------------

function clientSelectedHandler( data )
{
	if( typeof ( data ) == "undefined" )
		return;

	// 有效用户检测
	if( data.invalid == 1 || data.source == "cru0943rdrf2" || data.category == "510a4qf1i57p" )
	{
		infopad.err( "无效、未知、广告推销类型的客户无须添加学员资料。" );
		return;
	}

	setClientData( data );
}

function setClientData( data )
{
	// 客户资料
	if( typeof ( data ) == "object" && data.nickname )
	{
		if( empty( data.client ) )
			clientKey = data.key;
		else
			clientKey = data.client;

		$( "#nickname" ).html(
				data.nickname
						+ ( data.invalid == 1 ? "<em class='caption'>（无效客户）</em>"
								: "<em class='valid'>（有效客户）</em>" ) );
		$( "#category" ).html( shtml( data.categoryns ) );
		$( "#source" ).html( shtml( data.sourcens ) );
		$( "#keyword" ).html( shtml( data.keyword ) );
		$( "#address" ).html( data.province + " " + data.city + " " + data.address );
		$( "#phone" ).html( shtml( data.phone ) );
		$( "#email" ).html( shtml( data.email ) );
		$( "#qq" ).html( shtml( data.qq ) );
		$( "#ip" ).html( shtml( data.ip ) );
	}
	else
	{
		clientKey = null;

		$( "#nickname" ).html( "&nbsp;" );
		$( "#category" ).html( "&nbsp;" );
		$( "#source" ).html( "&nbsp;" );
		$( "#address" ).html( "&nbsp;" );
		$( "#keyword" ).html( "&nbsp;" );
		$( "#address" ).html( "&nbsp;" );
		$( "#phone" ).html( "&nbsp;" );
		$( "#email" ).html( "&nbsp;" );
		$( "#qq" ).html( "&nbsp;" );
		$( "#ip" ).html( "&nbsp;" );
	}
}

function setPaymentData( data )
{
	if( !empty( data ) )
	{
		key = data.key;

		$( "#paydate" ).val( data.date );
		$( "#cost" ).val( data.cost );
		$( "#discount" ).val( data.discount );
		$( "#serial" ).val( data.serial );
		$( "#xltype" ).val( data.type );
		$( "#region" ).val( data.region );
		$( "#remark" ).html( data.remark );

		client_detailed( $( "#tbl_payment" ), data.type == 1 ? false : true );
	}
	else
	{
		key = null;

		$( "#paydate" ).val( today() );
		$( "#cost" ).val( "" );
		$( "#discount" ).val( "" );
		$( "#serial" ).val( "" );
		$( "#xltype" ).val( 0 );
		$( "#region" ).val( "" );
		$( "#remark" ).html( "" );

		client_detailed( $( "#tbl_payment" ), true );
	}
}

function shtml( val )
{
	if( empty( val ) )
		return "<em class='caption'>未填写</em>";
	else if( val == '--' )
		return "<em class='caption'>禁止查看</em>";
	return val;
}

function submit_form()
{
	var data = {};

	// 客户key
	data.client = clientKey;
	if( empty( data.client ) )
	{
		infopad.err( "未指定需要缴费的目标客户。" );
		return;
	}

	// 待更新的接待数据
	data.key = key;
	data.cost = $( "#cost" ).val();
	data.discount = $( "#discount" ).val();
	data.serial = $( "#serial" ).val();
	data.type = $( "#xltype" ).val();
	data.region = $( "#region" ).val();
	data.remark = htmlfilter( $( "#remark" ).html() );

	submitter.post( "consultant_payment_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );
			key = d.key;
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}

//--------------------------------------------------------------------------
//
//	methods
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submit_form();
	}
	else if( e.ctrlKey && e.shiftKey && e.which == 70 )// ctrl+shift+f
	{
		var edt = $( document.activeElement );
		var id = edt.attr( "id" );

		if( _editor.indexOf( id ) > -1 )
		{
			edt.html( htmlfilter( edt.html() ) );
		}
	}
}
